package com.example.wcdb;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

import com.example.wcdb.config.DbHelper;
import com.example.wcdb.dao.UserDao;
import com.example.wcdb.entity.User;

import java.io.File;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 数据测试页面（增删改查）
 * @author zhangxin221
 */
public class TestDataActivity extends AppCompatActivity {
    private final RepairableDatabase database = RepairableDatabase.getDatabase();
    private final ExecutorService executor = Executors.newSingleThreadExecutor();
    private TextView tvMsg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_data);
        tvMsg = findViewById(R.id.tv_msg);

        findViewById(R.id.btn_add).setOnClickListener((v) -> {
            executor.execute(() -> {
                UserDao userDao = database.getUserDao();
                User user = new User();
                user.id = userDao.selectMaxId() + 1;
                user.userId = String.valueOf(user.id);
                user.userName = "testUser" + user.userId;
                user.age = 10;
                String msg;
                if (database.getUserDao().insert(user) > 0) {
                    msg = "插入操作成功";
                } else {
                    msg = "插入操作失败";
                }
                setMsg(msg);
            });
        });

        findViewById(R.id.btn_query).setOnClickListener((v) -> {
            executor.execute(() -> {
                showQueryDialog(database.getUserDao().selectAllUserNames());
            });
        });

        findViewById(R.id.btn_delete).setOnClickListener((v) -> {
            executor.execute(() -> {
                String msg;
                if (database.getUserDao().deleteAll() > 0) {
                    msg = "删除操作成功";
                } else {
                    msg = "删除操作失败";
                }
                setMsg(msg);
            });
        });

        findViewById(R.id.btn_update).setOnClickListener((v) -> {
            executor.execute(() -> {
                UserDao userDao = database.getUserDao();
                List<User> users = userDao.selectAll();
                for (User user : users) {
                    user.userName = "update_" + user.userName;
                }
                String msg;
                if (database.getUserDao().update(users) > 0) {
                    msg = "更新操作成功";
                } else {
                    msg = "更新操作失败";
                }
                setMsg(msg);
            });
        });
    }

    private void showQueryDialog(String[] data) {
        runOnUiThread(()-> {
            new AlertDialog.Builder(this)
                    .setTitle("查询结果")
                    .setItems(data, null)
                    .show();
        });
    }

    private void setMsg(String msg) {
        runOnUiThread(() -> tvMsg.setText(msg));
    }
}